package com.lxmes.sales.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.NcClientFiles;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.sales.domain.NcQuotedPrice;
import com.lxmes.sales.service.INcQuotedPriceService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 报价单Controller
 * 
 * @author ruoyi
 * @date 2022-11-26
 */
@RestController
@RequestMapping("/sales/price")
public class NcQuotedPriceController extends BaseController
{
    @Autowired
    private INcQuotedPriceService ncQuotedPriceService;

    /**
     * 查询报价单列表
     */
    @PreAuthorize("@ss.hasPermi('sales:price:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcQuotedPrice ncQuotedPrice)
    {
        startPage();
        List<NcQuotedPrice> list = ncQuotedPriceService.selectNcQuotedPriceList(ncQuotedPrice);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('sales:price:list')")
    @GetMapping("/listRules")
    public TableDataInfo listRules(NcQuotedPrice ncQuotedPrice)
    {
        startPage();
        List<NcQuotedPrice> list = ncQuotedPriceService.selectNcQuotedPriceListByMaterial(ncQuotedPrice);
        return getDataTable(list);
    }

    /**
     * 导出报价单列表
     */
    @PreAuthorize("@ss.hasPermi('sales:price:export')")
    @Log(title = "报价单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcQuotedPrice ncQuotedPrice)
    {
        List<NcQuotedPrice> list = ncQuotedPriceService.selectNcQuotedPriceList(ncQuotedPrice);
        ExcelUtil<NcQuotedPrice> util = new ExcelUtil<NcQuotedPrice>(NcQuotedPrice.class);
        util.exportExcel(response, list, "报价单数据");
    }

    /**
     * 获取报价单详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:price:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncQuotedPriceService.selectNcQuotedPriceById(id));
    }

    @PreAuthorize("@ss.hasPermi('sales:price:query')")
    @GetMapping(value = "/getInfoByDh/{bjdh}")
    public AjaxResult getInfoByDh(@PathVariable("bjdh") String bjdh)
    {
        return AjaxResult.success(ncQuotedPriceService.selectNcQuotedPriceBybjdH(bjdh));
    }



    /**
     * 新增报价单
     */
    @PreAuthorize("@ss.hasPermi('sales:price:add')")
    @Log(title = "报价单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcQuotedPrice ncQuotedPrice)
    {
        return toAjax(ncQuotedPriceService.insertNcQuotedPrice(ncQuotedPrice));
    }

    /**
     * 修改报价单
     */
    @PreAuthorize("@ss.hasPermi('sales:price:edit')")
    @Log(title = "报价单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcQuotedPrice ncQuotedPrice)
    {
        return toAjax(ncQuotedPriceService.updateNcQuotedPrice(ncQuotedPrice));
    }
    @PreAuthorize("@ss.hasPermi('sales:price:edit')")
    @Log(title = "报价单", businessType = BusinessType.UPDATE)
    @GetMapping("/updatIshs")
    public AjaxResult updatIshs(NcQuotedPrice ncQuotedPrice)
    {
        return toAjax(ncQuotedPriceService.updateNcQuotedPrice(ncQuotedPrice));
    }

    /**
     * 生成合同
     */
    @PreAuthorize("@ss.hasPermi('sales:price:edit')")
    @GetMapping("/scht/{ids}")
    public AjaxResult scht(@PathVariable("ids") String ids)
    {

        List<NcQuotedPrice> qPricesByIds = ncQuotedPriceService.getQPricesByIds(ids);
        //1:判断有没有单号不一样的
        List newList = qPricesByIds.stream().map(NcQuotedPrice::getBjdH).distinct().collect(Collectors.toList());
        if(newList.size()>1){
            return AjaxResult.error("选择的单号不是同一单号，请重新选择！");
        }
        if(qPricesByIds.get(0).getSchtbj().equals("1")){
            return AjaxResult.error("所选报价单中已有生成了合同,不能重复生成。");
        }
        try {
            for(NcQuotedPrice ncQuotedPrice : qPricesByIds){
                ncQuotedPriceService.scht(ncQuotedPrice);
            }
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
        return AjaxResult.success("生成成功！");
    }

    /**
     * 删除报价单
     */
    @PreAuthorize("@ss.hasPermi('sales:price:remove')")
    @Log(title = "报价单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncQuotedPriceService.deleteNcQuotedPriceByIds(ids));
    }


    /**
     * 生成报价单号
     */
    @GetMapping("/genBjNumber")
    public AjaxResult genBjNumber(){
        return AjaxResult.success("",ncQuotedPriceService.genBjNumber());
    }

    @Log(title = "报价管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {  ExcelUtil<NcQuotedPrice> util = new ExcelUtil<>(NcQuotedPrice.class);
        List<NcQuotedPrice> priceList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncQuotedPriceService.imporIteminfo(priceList, updateSupport, operName);

        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcQuotedPrice> util = new ExcelUtil<>(NcQuotedPrice.class);
        util.importTemplateExcel(response,"报价数据");
    }

}
